package cn.bigway.app.weavingdesigner.weaving;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import cn.bigway.app.weavingdesigner.util.ButtonConcaveBorder;
import cn.bigway.app.weavingdesigner.util.ButtonConvexBorder;

public class WdDocColorPanel extends JPanel {
	private static final long serialVersionUID = 668409168671383633L;

	private class ColorButton extends JPanel {
		private static final long serialVersionUID = 6562732879571834101L;
		private Color color;

		protected ColorButton(Color color) {
			this.color = color;
			setBackground(color);
			setBorder(new ButtonConcaveBorder());
			setPreferredSize(new Dimension(18, 18));
		}

		protected Color getColor() {
			return this.color;
		}
	}

	protected class ButtonMouseListener extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent event) {
			ColorButton button = (ColorButton) event.getSource();
			if (event.getModifiers() == InputEvent.BUTTON1_MASK) {
				setFrontColor(button.getColor());
			} else if (event.getModifiers() == InputEvent.BUTTON3_MASK) {
				setBackColor(button.getColor());
			}
		}
	}

	private class ViewColorPanel extends JPanel {
		private static final long serialVersionUID = -8552615264282713355L;

		protected ViewColorPanel() {
			setPreferredSize(new Dimension(14, 14));
			setBackground(Color.BLACK);
			setBorder(new ButtonConvexBorder());
		}
	}

	private class ViewPanel extends JLayeredPane {
		private static final long serialVersionUID = -197645866215181994L;
		private ViewColorPanel front, back;

		protected ViewPanel() {
			setLayout(null);
			setPreferredSize(new Dimension(30, 30));
			setBackground(new Color(250, 250, 250));
			setBorder(new ButtonConcaveBorder());

			this.back = new ViewColorPanel();
			this.front = new ViewColorPanel();

			Insets insets = getInsets();

			Dimension size = this.front.getPreferredSize();
			this.front.setBounds(1 + insets.left, 2 + insets.top, size.width,
					size.height);

			size = this.back.getPreferredSize();
			this.back.setBounds(9 + insets.left, 10 + insets.top, size.width,
					size.height);

			add(this.front, new Integer(1));
			add(this.back, new Integer(0));
		}

		protected void setFrontColor(Color color) {
			this.front.setBackground(color);
		}

		protected void setBackColor(Color color) {
			this.back.setBackground(color);
		}
	}

	private ViewPanel viewPanel;
	private Vector<ColorButton> colorButtons;
	private ButtonMouseListener buttonMouseListener;

	public WdDocColorPanel() {
		this.viewPanel = new ViewPanel();
		add(this.viewPanel);

		this.buttonMouseListener = new ButtonMouseListener();

		this.colorButtons = new Vector<ColorButton>();
		this.colorButtons.add(new ColorButton(Color.BLACK));
		this.colorButtons.add(new ColorButton(Color.WHITE));
		this.colorButtons.add(new ColorButton(Color.RED));
		this.colorButtons.add(new ColorButton(Color.MAGENTA));
		this.colorButtons.add(new ColorButton(Color.PINK));
		this.colorButtons.add(new ColorButton(Color.YELLOW));
		this.colorButtons.add(new ColorButton(Color.GREEN));
		this.colorButtons.add(new ColorButton(Color.CYAN));
		this.colorButtons.add(new ColorButton(Color.BLUE));

		for (ColorButton button : this.colorButtons) {
			button.addMouseListener(this.buttonMouseListener);
			add(button);
		}
	}

	protected void setFrontColor(Color color) {
		this.viewPanel.setFrontColor(color);
	}

	protected void setBackColor(Color color) {
		this.viewPanel.setBackColor(color);
	}
}
